package net.newsmth.smthapp.android.helper;


import net.newsmth.smthapp.android.R;
import net.newsmth.smthapp.android.config.SmthConnection;
import net.newsmth.smthapp.android.model.BBSThread;
import net.newsmth.smthapp.android.model.Section;
import net.newsmth.smthapp.android.model.SectionType;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hatsune Miku on 4/12/14.
 */
public class SectionHelperImpl implements SectionHelper {
    private List<Section> sections;
    private static int SECTION_COUNT = 11;

    private SmthConnection conn;

    public SectionHelperImpl() {}

    public SectionHelperImpl(SmthConnection connection) {
        this.conn = connection;
    }

    @Override
    public List<Section> getAllSections() {
        if (sections == null) {
            sections = new ArrayList<Section>(SECTION_COUNT);

            makeAllSections();
        }
        return sections;
    }

    private void makeAllSections() {
        sections.add(new Section(SectionType.TOP_TEN.getSectionId(), R.string.section_top_ten));
        sections.add(new Section(SectionType.CHINA_COLLEGE.getSectionId(), R.string.section_china_college));
        sections.add(new Section(SectionType.ENTERTAINMENT.getSectionId(), R.string.section_entertainment));
        sections.add(new Section(SectionType.REGION.getSectionId(), R.string.section_region));
        sections.add(new Section(SectionType.GAME_SPORT.getSectionId(), R.string.section_game_sport));
        sections.add(new Section(SectionType.SOCIETY.getSectionId(), R.string.section_society));
        sections.add(new Section(SectionType.EMOTIONS.getSectionId(), R.string.section_emotions));
        sections.add(new Section(SectionType.HUMANITY.getSectionId(), R.string.section_humanity));
        sections.add(new Section(SectionType.SCIENCE.getSectionId(), R.string.section_science));
        sections.add(new Section(SectionType.COMPUTER.getSectionId(), R.string.section_computer));
    }

    /**
     * Get the section of boards.
     * @param sectionId The section id, you can get it from {@link net.newsmth.smthapp.android.model.SectionType}
     * @return the section which has the section id
     */
    @Override
    public Section getSection(long sectionId) {
        List<Section> allSections = getAllSections();

        for (Section section : allSections) {
            if (section.getId() == sectionId) {
                return section;
            }
        }

        return null;
    }

    @Override
    public List<BBSThread> getThreadsFromSection(long sectionId) {
        JSONArray threadJsonArray = conn.getSectionHot(sectionId);
        List<BBSThread> threads = new ArrayList<BBSThread>();

        if (threadJsonArray != null) {
            for (int i = 0; i < threadJsonArray.length(); i++) {
                BBSThread thread = null;

                try {
                    thread = BBSThread.convertFromJSON(threadJsonArray.getJSONObject(i));
                } catch (Exception e) {
                }

                if (thread != null) {
                    threads.add(thread);
                }
            }
        }

        return  threads;
    }

    @Override
    public BBSThread getThreadFromSection(long sectionId, long threadId) {
        List<BBSThread> threads = getThreadsFromSection(sectionId);

        for (BBSThread thread : threads) {
            if (thread.getId() == threadId) {
                return  thread;
            }
        }

        return  null;
    }
}
